package com.neuedu.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Component
@Aspect
public class AopAdvice {

    @Pointcut("execution(* com.neuedu.service.*.insert(..))")
    public void pointcut(){};


    //@Before("pointcut()")
    public void before(){
        System.out.println("使用注解的形式，[前置]声明的通知");
    }


    //@After("pointcut()")
    public void after(){ System.out.println("使用注解的形式，[后置]声明的通知");
    }

    //@AfterReturning("pointcut()")
    public void afterReturn(){ System.out.println("使用注解的形式，[返回通知]声明的通知");
    }


    @AfterThrowing(pointcut ="pointcut()", throwing="ex")
    public void afterEx(Exception ex){
        System.out.println(ex);
        System.out.println("使用注解的形式，[异常通知]声明的通知");
    }


    //spring-tx
//    环绕通知
    @Around("pointcut()")
    public void around(ProceedingJoinPoint pjp){
        //之前
        //insert 切点
        System.out.println("1 around    开启事务 ...........");
        try {
            pjp.proceed();  //insert方法
            System.out.println("2提交事务");
        } catch (Throwable throwable) {
            System.out.println("3回滚事务");
            throwable.printStackTrace();
        }
        System.out.println("4 around    end   回收 资源...........");
        //之后
    }
}